#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "./getch/getch.h"

#define MAXWORD 100
#define NKEYS (sizeof keytab / sizeof(struct key))

struct key
{
	char *word;
	int count;
};

struct key keytab[] = {
	{"auto", 0},
	{"break", 0},
	{"case", 0},
	{"char", 0},
	{"const", 0}
	// ..........
};

int getword(char *, int);
int binsearch(char *, struct key *, int);

// count C keywords
int main()
{
	int n;
	char word[MAXWORD];

	while(getword(word, MAXWORD) != EOF)
		if(isalpha(word[0]))		// 识别是否是字母
			if((n = binsearch(word, keytab, NKEYS)) >= 0)
				keytab[n].count++;
	for(n = 0; n < NKEYS; n++)
		if(keytab[n].count > 0)
			printf("%4d %s \n", keytab[n].count, keytab[n].word);

	return 0;
}

// binsearch: find word in tab[0] ... tab[n-1]
int binsearch(char *word, struct key tab[], int n)
{
	int cond;
	int low, high, mid;

	low = 0;
	high = n - 1;
	while(low <= high)
	{
		mid = (low + high) / 2;
		if((cond = strcmp(word, tab[mid].word)) < 0)
			high = mid - 1;
		else if(cond > 0)
			low = mid + 1;
		else 
			return mid;
	}
	return -1;
}

// getword: get next word or character from input
int getword(char *word, int lim)
{
	int c, getch(void);
	void ungetch(int);
	char *w = word;

	while(isspace(c = getch()))		// 识别是否是空白符
		;
	if(c != EOF)
		*w++ = c;
	if(!isalpha(c))				// 识别是否是字母
	{
		*w = '\0';
		return c;
	}
	for(; --lim > 0; w++)
		if(!isalnum(*w = getch()))	// 识别是否是字母和数字
		{
			ungetch(*w);
			break;
		}
	*w = '\0';
	return word[0];
}
